/**
 * @author Dalia
 */
package controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import models.*;

import com.google.gson.JsonObject;

import play.mvc.Controller;
import play.mvc.With;
import controllers.Secure.Security;

@With(Secure.class)
public class MarkAsDuplicatesController extends Controller {


	/**
	 * Takes the id of the original idea and renders it to the view
	 * 
	 * @param id
	 *            The id of the idea that we wish to mark as a duplicate that is
	 *            to be rendered to the view
	 */
	public static void markDuplicates(long id) {
		render(id);
	}

	/**
	 * Takes the id of the idea that we wish to mark and sends the data of all
	 * other ideas in the same topic/entity/organization as the original idea to
	 * the view
	 * 
	 * @param id
	 *            The id of the idea that we wish to mark as a duplicate
	 */
	public static void displayIdeas(long id) {
		List<Idea> list = new ArrayList<Idea>();
		Idea idea = (Idea) Idea.find("id = ?", id).first();
		if (idea.ideaTopic != null) {
			list = idea.ideaTopic.ideas;
			list.remove(idea);
		} else {
			if (idea.ideaEnt != null) {
				list = idea.ideaEnt.ideas;
				list.remove(idea);
			} else {
				if (idea.ideaOrg != null) {
					list = idea.ideaOrg.ideas;
					list.remove(idea);
				}
			}
		}
		List<Idea> tempList = new ArrayList<Idea>();
		for (int i = 0; i < list.size(); i++) {
			if (!(idea.duplicates.contains(list.get(i)))
					&& !(list.get(i).duplicates.contains(idea))) {
				tempList.add(list.get(i));
			}
		}

		String ids = "";
		String titles = "";
		String descriptions = "";
		if (tempList.size() == 0) {
			titles += "There are no other ideas to mark!!";
		}
		for (int i = 0; i < tempList.size(); i++) {
			ids += "&" + tempList.get(i).id;
			titles += "&" + tempList.get(i).title;
			descriptions += "&" + tempList.get(i).description;
		}

		JsonObject duplicateIdeas = new JsonObject();
		duplicateIdeas.addProperty("titles", titles);
		duplicateIdeas.addProperty("ids", ids);
		duplicateIdeas.addProperty("descriptions", descriptions);
		renderJSON(duplicateIdeas.toString());
	}

	/**
	 * Marks two Ideas as duplicates of each other
	 * 
	 * @param duplicate
	 *            the duplicate idea's id
	 * @param original
	 *            the original idea's id
	 */
	public static void markDuplicatesInIdea(String duplicate, String original) {

		long duplicateId = Long.parseLong(duplicate);
		long originalId = Long.parseLong(original);
		Idea originalIdea = (Idea) Idea.find("id = ?", originalId).fetch()
				.get(0);
		Idea duplicateIdea = (Idea) Idea.find("id = ?", duplicateId).fetch()
				.get(0);
		originalIdea.duplicates.add(duplicateIdea);
		originalIdea.save();
		duplicateIdea.duplicatesOf.add(originalIdea);
		duplicateIdea.save();

		Organization orgOfIdea = originalIdea.orgWhereIdeaExist();
		IdeaDeveloper organizationLead = orgOfIdea.creator.ideaDeveloper;
		String user = Security.connected();
		// String user = "unique";
		IdeaDeveloper currentUser = (IdeaDeveloper) IdeaDeveloper.find(
				"username like ?", user).first();
		String notification = " has marked Idea: <a href='/topicideacontroller/checkAuthorizationToViewIdea?ideaId="
								+ originalIdea.id + "'>" + originalIdea.title + "</a>"  
				+ " as a duplicate with <a href='/topicideacontroller/checkAuthorizationToViewIdea?ideaId="
								+ duplicateIdea.id + "'>" + duplicateIdea.title + "</a>";
		Notification.notify(notification, organizationLead, currentUser,
				new Date());
		Log newEntry = new Log("^ has marked ^ with ^ as duplicates");
		newEntry.ids = currentUser.id + "," + originalIdea.id + ","
				+ duplicateIdea.id;
		newEntry.types = "developer,idea,idea";
		newEntry.save();

		String ids = "";
		String titles = "";
		if (originalIdea.duplicates.size() == 0) {
			titles += "NOOOO!!!";
		}
		for (int i = 0; i < originalIdea.duplicates.size(); i++) {
			ids += "&" + originalIdea.duplicates.get(i).id;
			titles += "&" + originalIdea.duplicates.get(i).title;
		}

		JsonObject duplicateIdeas = new JsonObject();
		duplicateIdeas.addProperty("titles", titles);
		duplicateIdeas.addProperty("ids", ids);
		renderJSON(duplicateIdeas.toString());

	}

}